package com.example.sqliterecyclerview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class EditActivity extends AppCompatActivity {
    private EditText edtNo,edtName,edtAge;
    private Button btnSave;
    private String stuno;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        Intent intent = getIntent();
        stuno = intent.getStringExtra("STUNO");
        String name = intent.getStringExtra("NAME");
        int age = intent.getIntExtra("AGE",0);

        edtNo = findViewById(R.id.edt_no);
        edtName = findViewById(R.id.edt_name);
        edtAge = findViewById(R.id.edt_age);
        btnSave = findViewById(R.id.btn_save);

        edtNo.setText(stuno);
        edtName.setText(name);
        edtAge.setText(String.valueOf(age));

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MySQLiteOpenHelper helper = new MySQLiteOpenHelper(EditActivity.this,"stu.db",null,1);
                SQLiteDatabase writableDatabase = helper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name",edtName.getText().toString());
                values.put("age",Integer.parseInt(edtAge.getText().toString()));
                String where = "stuno = " + stuno;
                writableDatabase.update("student",values,where,null);
                writableDatabase.close();
                finish();
            }
        });
    }
}